import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class CreateUser {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//http://localhost/testlink-1.9.1/
		CreateUser userCreator = new CreateUser();
		//userCreator.newUser("Vorname2", "Nachmane2", "test@test.de", "Novell12");
		//userCreator.newUser("Vorname1", "Nachmane1", "tes1t@test.de", "Novell13");
	}
	WebDriver driver;
	
	public CreateUser(){
		driver = new BasfFirefoxDriver();
		driver.get("http://bistrotesting.basf.net");
		WebElement user = driver.findElement(By.name("tl_login"));
		WebElement pwd = driver.findElement(By.name("tl_password"));
		user.sendKeys("admin");
		pwd.sendKeys("F1d3S7881");
		pwd.submit();
	}
	public void userManager(){
		driver.get("http://bistrotesting.basf.net/lib/usermanagement/usersView.php");
	}
	public void newUser(String vorname, String nachname, String email, String id){
		this.userManager();
		
		driver.findElement(By.name("doCreate")).click();
		WebElement novell = driver.findElement(By.name("login"));
		WebElement firstname = driver.findElement(By.name("firstName"));
		WebElement surname = driver.findElement(By.name("lastName"));
		WebElement pwd = driver.findElement(By.name("password"));
		WebElement mail = driver.findElement(By.id("email"));
		WebElement save = driver.findElement(By.name("do_update"));
		WebElement rightsDrop = driver.findElement(By.xpath("//html/body/div[2]/form/fieldset/table/tbody/tr[6]/td/select"));
		rightsDrop.click();
		List<WebElement> rights = driver.findElements(By.xpath("//html/body/div[2]/form/fieldset/table/tbody/tr[6]/td/select/option"));
		long wait = System.currentTimeMillis() + 2000;
		while(wait > System.currentTimeMillis());
		rights.get(6).setSelected();
		rightsDrop.sendKeys(Keys.ENTER);
		novell.sendKeys(id);
		WebElement languageDrop = driver.findElement(By.xpath("//html/body/div[2]/form/fieldset/table/tbody/tr[7]/td/select"));
		languageDrop.click();
		List<WebElement> language = driver.findElements(By.xpath("//html/body/div[2]/form/fieldset/table/tbody/tr[7]/td/select/option"));
		wait = System.currentTimeMillis() + 2000;
		while(wait > System.currentTimeMillis());
		language.get(2).setSelected();
		languageDrop.sendKeys(Keys.ENTER);
		firstname.sendKeys(vorname);
		surname.sendKeys(nachname);
		pwd.sendKeys(id);
		mail.sendKeys(email);
		save.click();
	}

}
